package com.bootdo.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.KswUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.RoleDO;
import com.bootdo.system.domain.TbStock;
import com.bootdo.system.domain.TbSvip;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.dto.GmspDto;
import com.bootdo.system.dto.ProductDto;
import com.bootdo.system.service.KswService;
import com.bootdo.system.service.ProductService;
import com.bootdo.system.service.RoleService;

@RequestMapping("/product")
@Controller
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private KswService kswService;
	
	String prefix = "system/product";
	
	@GetMapping()
	String product() {
		return prefix + "/product";
	}
	
	@GetMapping("goods")
	String goods() {
		return prefix + "/goods";
	}
	
	@ResponseBody
	@RequestMapping("/goodsList")
	public  List<TbSvip> goodsList() {
		 return productService.getSvipList();
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public PageUtils gzhConfigList(@RequestParam Map<String, Object> params) {
		UserDO user = ShiroUtils.getUser();
		List<RoleDO> list2 = roleService.getRoleByUserId(user.getUserId());
		boolean falg=false;
		for (RoleDO role : list2) {
			if(role.getRoleId() == 59L) {
				falg=true;
			}
		}
		if(falg) {
			params.put("deptId", user.getDeptId());
		}else {
			params.put("userId", user.getUserId());
		}
		Query query = new Query(params);
		 List<Map<String,Object>> list = productService.list(query);
		 int count = productService.count(query);
		 PageUtils pageUtil = new PageUtils(list, count);
		 return pageUtil;
	}
	
	
	@GetMapping("/edit/{id}")
	String edit(Model model,@PathVariable("id") Integer id) {
		TbStock us = productService.getByStoId2(id);
		List<TbSvip> svipList = productService.getSvipList();
		model.addAttribute("svip", svipList);
		model.addAttribute("us", us);
		return prefix+"/edit";
	}
	
	@GetMapping("/editGoods/{id}")
	String editGoods(Model model,@PathVariable("id") Integer id) {
		TbSvip us = productService.getById(id);
		model.addAttribute("us", us);
		return prefix+"/goodsEdit";
	}
	
	@GetMapping("/add")
	String add(Model model) {
		List<TbSvip> svipList = productService.getSvipList();
		model.addAttribute("svip", svipList);
		return prefix+"/add";
	}
	
	@GetMapping("/addGoods")
	String addGoods(Model model) {
		List<TbSvip> svipList = productService.getSvipList();
		model.addAttribute("svip", svipList);
		return prefix+"/goodsAdd";
	}
	
	
	@PostMapping("/update")
	@ResponseBody
	R update(TbSvip table) {
		if (productService.insert2update(table) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/updateCp")
	@ResponseBody
	R updateCp(TbStock table) {
		String svipId = table.getSvipId();
		TbSvip byId = productService.getById(Integer.valueOf(svipId));
		Long userId = ShiroUtils.getUserId();
		table.setMoney(byId.getPrice()+"");
		table.setSid(String.valueOf(userId));
		if (productService.insert2updateSto(table) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/getPrice")
	@ResponseBody
	R getPrice(Integer id) {
		String customer_id=KswUtils.customer_id;
		String key=KswUtils.ksw_key;
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhss");//设置日期格式
		String timestamp = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
		 TreeMap<String, String> treeMap = new TreeMap<String, String>();
		   treeMap.put("customer_id" , customer_id);
		   treeMap.put("timestamp" , timestamp);
		   ProductDto spxx = kswService.getSpxx(id,key, treeMap);
		   if(spxx != null) {
			   return R.ok(spxx.getPrice()+"");
		   }
		return R.error();
	}
	
	
	@PostMapping("/remove")
	@ResponseBody
	R remove(Integer id) {
		if (productService.removeSto(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/removeGoods")
	@ResponseBody
	R removeGoods(Integer id) {
		if (productService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Integer[] ids) {
		int r = productService.batchremoveSto(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/batchRemoveGoods")
	@ResponseBody
	R batchRemoveGoods(@RequestParam("ids[]") Integer[] ids) {
		int r = productService.batchremove(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}

}
